package com.akk.module.upload.service;

import com.akk.module.upload.utils.FtpUtil;
import com.akk.utils.IDUtils;
import org.joda.time.DateTime;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class PictureService {
	
	@Value("${ftp.address}")
	private String FTP_ADDRESS;
	@Value("${ftp.port}")
	private Integer FTP_PORT;
	@Value("${ftp.username}")
	private String FTP_USERNAME;
	@Value("${ftp.password}")
	private String FTP_PASSWORD;
	@Value("${ftp.base.path}")
	private String FTP_BASE_PATH;
	@Value("${image.base.url}")
	private String IMAGE_BASE_URL;



	public Map uploadPicture(MultipartFile uploadFile) {
		Map resultMap = new HashMap<>();
		try {
			//生成一个新的文件名
			//取原始文件名
			String oldName = uploadFile.getOriginalFilename();
			//生成新文件名
			//UUID.randomUUID();
			String newName = IDUtils.genImageName();
			newName = newName + oldName.substring(oldName.lastIndexOf("."));
			//图片上传
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD,
					FTP_BASE_PATH, imagePath, newName, uploadFile.getInputStream());
			//返回结果
			if(!result) {
				resultMap.put("status", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("status", 0);
			resultMap.put("url", IMAGE_BASE_URL + imagePath + "/" + newName);
			return resultMap;
			
		} catch (Exception e) {
			resultMap.put("status", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}
	@Test
	public void test() {

		try {
			FileInputStream in = new FileInputStream(new File(
					"F:\\person.jpg"));
			boolean flag = FtpUtil.uploadFile("192.168.1.179", 21, "ftpuser",
					"ftpuser","/home/ftpuser/picture",new DateTime().toString("/yyyy/MM/dd"),
					"person.jpg", in);
			System.out.println(flag);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Map uploadBranchPicture(MultipartHttpServletRequest multiRequest) {
		String picturePath = "";
		Map result = new HashMap<>();
		Iterator<String> iter = multiRequest.getFileNames();
		while(iter.hasNext()){
			MultipartFile file = multiRequest.getFile(iter.next());
			if(file != null){
				result = uploadPicture(file);
				if((Integer)result.get("status")==0){
					picturePath = result.get("url").toString()+",";
				}
			}
		}
		picturePath = picturePath.substring(0,picturePath.length()-1);
		result.put("url",picturePath);
		return result;
	}

	public boolean removeFile(String url){
		if (!StringUtils.hasText(url) || url.length() < 9)
			return false;
		url = url.substring(url.indexOf("/picture/") + 1,url.length());
		return FtpUtil.removeFile(FTP_ADDRESS,FTP_PORT,FTP_USERNAME,FTP_PASSWORD,url);
	}

	/*只能用于身份证图片上传*/
	public String uploadPictureByAuth(byte[] bytes) {
		//生成新文件名
		//UUID.randomUUID();
		String newName = IDUtils.genImageName();
		newName = newName+".jpeg";
		//图片上传
		String imagePath = new DateTime().toString("/yyyy/MM/dd");
		boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD,
				FTP_BASE_PATH, imagePath, newName, new ByteArrayInputStream(bytes));
		if(result)
			return IMAGE_BASE_URL + imagePath + "/" + newName;
		return null;
	}
}
